package org.vordel.incubator.examples.management_api;

import java.util.ArrayList;
import java.util.Collection;

import com.vordel.es.ESPK;
import com.vordel.es.Entity;
import com.vordel.es.EntityStore;

public class WebService extends EntityWrapper {

	private ArrayList<WSDL> wsdls = new ArrayList<WSDL>();
	private WSDL primaryWSDL;
	private OriginalWebService originalWebService;
	
	WebService(Entity entity, EntityWrapperManager manager) throws Exception {
		super(entity, manager);
		
		EntityStore es = manager.session.getEntityStore();
		// TODO Auto-generated constructor stub
		ESPK primaryWSDLESPK = this.entity.getReferenceValue("primaryWSDL");
		
		if (primaryWSDLESPK == es.ES_NULL_PK) {
			return;
		}
		
		this.primaryWSDL = new WSDL(manager.session.getEntityStore().getEntity(primaryWSDLESPK), manager);
		
		//This seems to be an issue when a Web Service is created
		if (this.entity.getPK()==null) {
			return;
		}
		
		Collection<ESPK> childOriginalWebServices = es.listChildren(this.entity.getPK(), es.getTypeForName("OriginalWebService"));
		
		if (childOriginalWebServices.size()!=0) {
			this.originalWebService = new OriginalWebService(es.getEntity(childOriginalWebServices.iterator().next()), manager);
		}
		
		
		
	}
	
	
	public WSDL getPrimaryWSDL() {
		return this.primaryWSDL;
	}

	public OriginalWebService getOriginalWebService() {
		return originalWebService;
	}

	public Schema addSchema(String url, String location, byte [] contents) throws Exception {
		
		return ((WebServiceGroupManager)this.manager).createSchema(url, location, contents, this.getEntity());
		
	}
	
}
